//
//  FQSimpleWKWebVC.m
//  KDFDApp
//
//  Created by Bruce on 2017/8/30.
//  Copyright © 2017年 cailiang. All rights reserved.
//

#import "FQSimpleWKWebVC.h"
#import "FQJumpAction.h"
#import "NSString+QCURLSchemeUtil.h"
static NSString *schemeKey = @"qunajie";
@interface FQSimpleWKWebVC ()
@property (nonatomic, assign) BOOL gobackCallback;
@property (nonatomic, assign) BOOL isFirstIn;
@end

@implementation FQSimpleWKWebVC
- (instancetype)init
{
    if (self = [super init]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(gobackCall:) name:@"QCWKWebGobackCallBack" object:nil];
    _isFirstIn = true;
    __weak typeof(self) weakSelf = self;
//    self.navDelegate.afterLoadFinish = ^(WKWebView *webView) {
//
//        //webview加载完毕 发出通知隐藏loading
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"webViewDidFinish" object:@{@"url":weakSelf.topWebView.URL.absoluteString ? : @""}];
//
//        //隐藏loadingview
////        [strongSelf hidenLoadingWithTime:0.1f];
//
//    };
//    self.navDelegate.didCommitNavigation = ^(WKWebView *webView, WKNavigation *navigation) {
//
//    };
//    self.navDelegate.afterLoadFaild = ^(WKWebView *webView, NSError *error) {
//
//        if (error.code == 0) {
//            return ;
//        }
//        //-999 NSURLErrorCancelled 链接被取消 NSURLErrorUnsupportedURL -1002
//        if (error.code == NSURLErrorCancelled || error.code == NSURLErrorUnsupportedURL) {
//            return ;
//        }
//    };
}
- (void)gobackCall:(NSNotification *)noc{
    NSDictionary *dic = noc.object;
    WKWebView *web = dic[@"webview"];
    //由于通知是一对多 所以必须要区分一下是不是本页面发的通知
    if (web == self.topWebView) {
         _gobackCallback = [dic[@"callback"] boolValue];
    }
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //如果web作为一级tab页面&&第一次进入页面的时候 不需要回调web
    if (self.from != 1 &&!self.isFirstIn) {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"webViewWillAppear" object:nil];
        //重置一下
        self.from = 0;
    }
    self.isFirstIn = false;
        __weak typeof(self) weakSelf = self;

//    //开始加载回调
//    self.navDelegate.didStartLoadWithRequest = ^(WKWebView *webView, WKNavigation *navigation) {
//
//        NSURL *URL = webView.URL;
//        NSString *scheme = [URL scheme];
//
//        weakSelf.gobackCallback = false;
//
//        weakSelf.URLString = webView.URL.absoluteString;
//
//        NSLog(@"========>>url=%@",URL.absoluteString);
//        if ([URL.absoluteString hasPrefix:@"https://itunes.apple.com"]||[URL.absoluteString hasPrefix:@"http://itunes.apple.com"]) {
//            [[UIApplication sharedApplication]openURL:URL];
////            return;
//        }
//        //跳转到支付宝
//        if ([URL.absoluteString containsString:@"alipay://"]||[URL.absoluteString containsString:@"alipays://"]) {
//            NSString *urlString = webView.URL.absoluteString;
//             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
//            return ;
//        }
//        if ([scheme isEqualToString:schemeKey]) {
//            NSMutableDictionary *dic = [FQJumpAction rsolve:webView.URL.absoluteString];
//            FQJumpAction *action = [FQJumpAction new];
//            [action jumpNew:dic entity:nil isPush:false];
//            return;
//        }
//
//        if (![scheme isEqualToString:@"http"]&&![scheme isEqualToString:@"https"]) {
//            [[UIApplication sharedApplication]openURL:URL];
//            return;
//        }
//        weakSelf.navigationController.navigationItem.rightBarButtonItem = nil;
//        weakSelf.navigationItem.rightBarButtonItem = nil;
//        weakSelf.topWebView.scrollView.scrollEnabled = YES;
//    };
//    //服务端重定向回调
//    self.navDelegate.didReceiveServerRedirect = ^(WKWebView *webView, WKNavigation *navigation) {
//        if ([webView.URL.absoluteString hasPrefix:@"https://itunes.apple.com"]||[webView.URL.absoluteString hasPrefix:@"http://itunes.apple.com"]) {
//            [[UIApplication sharedApplication]openURL:webView.URL];
//            return;
//        }
//        //
//        [webView.URL.absoluteString checkOpenApplicationURL];
//
//    };
//    self.navDelegate.decidePolicyForNavigationResponse = ^(WKWebView *webView, WKNavigationResponse *navigationResponse) {
//
//    };
//    //  根据WebView对于即将跳转的HTTP请求头信息和相关信息来决定是否跳转
//    self.navDelegate.decidePolicyForNavigationActionBlock = ^(WKWebView *webView, WKNavigationAction *navigationAction) {
//
//    };
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kShowProtetocolView" object:nil];
}

-(void)backAction:(id)sender
{
    if (!self.gobackCallback) {
        [super backAction:sender];
        
    }else{
        NSDictionary *dic = @{@"webview":self.topWebView ? : @""};
         [[NSNotificationCenter defaultCenter] postNotificationName:@"QCWKWebGobackClick" object:dic];
    }
}
- (void)closeAction:(id)sender
{
    [super closeAction:sender];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"QCWKWebGobackCallBack" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"kShowProtetocolView" object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
